package goquery

import (
	
	

	
)

// used to determine if a set (map[*html.Node]bool) should be used
// instead of iterating over a slice. The set uses more memory and
// is slower than slice iteration for small N.
const minNodesForSet = 1000

var nodeNames = []string{
	html.ErrorNode:    "#error",
	html.TextNode:     "#text",
	html.DocumentNode: "#document",
	html.CommentNode:  "#comment",
}

// NodeName returns the node name of the first element in the selection.
// It tries to behave in a similar way as the DOM's nodeName property
// (https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeName).
//
// Go's net/html package defines the following node types, listed with
// the corresponding returned value from this function:
//
//     ErrorNode : #error
//     TextNode : #text
//     DocumentNode : #document
//     ElementNode : the element's tag name
//     CommentNode : #comment
//     DoctypeNode : the name of the document type
//
func ( *Selection) string {
	if .Length() == 0 {
		return ""
	}
	return nodeName(.Get(0))
}

// nodeName returns the node name of the given html node.
// See NodeName for additional details on behaviour.
func nodeName( *html.Node) string {
	if  == nil {
		return ""
	}

	switch .Type {
	case html.ElementNode, html.DoctypeNode:
		return .Data
	default:
		if int(.Type) < len(nodeNames) {
			return nodeNames[.Type]
		}
		return ""
	}
}

// Render renders the HTML of the first item in the selection and writes it to
// the writer. It behaves the same as OuterHtml but writes to w instead of
// returning the string.
func ( io.Writer,  *Selection) error {
	if .Length() == 0 {
		return nil
	}
	 := .Get(0)
	return html.Render(, )
}

// OuterHtml returns the outer HTML rendering of the first item in
// the selection - that is, the HTML including the first element's
// tag and attributes.
//
// Unlike Html, this is a function and not a method on the Selection,
// because this is not a jQuery method (in javascript-land, this is
// a property provided by the DOM).
func ( *Selection) (string, error) {
	var  bytes.Buffer
	if  := Render(&, );  != nil {
		return "", 
	}
	return .String(), nil
}

// Loop through all container nodes to search for the target node.
func sliceContains( []*html.Node,  *html.Node) bool {
	for ,  := range  {
		if nodeContains(, ) {
			return true
		}
	}

	return false
}

// Checks if the contained node is within the container node.
func nodeContains( *html.Node,  *html.Node) bool {
	// Check if the parent of the contained node is the container node, traversing
	// upward until the top is reached, or the container is found.
	for  = .Parent;  != nil;  = .Parent {
		if  ==  {
			return true
		}
	}
	return false
}

// Checks if the target node is in the slice of nodes.
func isInSlice( []*html.Node,  *html.Node) bool {
	return indexInSlice(, ) > -1
}

// Returns the index of the target node in the slice, or -1.
func indexInSlice( []*html.Node,  *html.Node) int {
	if  != nil {
		for ,  := range  {
			if  ==  {
				return 
			}
		}
	}
	return -1
}

// Appends the new nodes to the target slice, making sure no duplicate is added.
// There is no check to the original state of the target slice, so it may still
// contain duplicates. The target slice is returned because append() may create
// a new underlying array. If targetSet is nil, a local set is created with the
// target if len(target) + len(nodes) is greater than minNodesForSet.
func appendWithoutDuplicates( []*html.Node,  []*html.Node,  map[*html.Node]bool) []*html.Node {
	// if there are not that many nodes, don't use the map, faster to just use nested loops
	// (unless a non-nil targetSet is passed, in which case the caller knows better).
	if  == nil && len()+len() < minNodesForSet {
		for ,  := range  {
			if !isInSlice(, ) {
				 = append(, )
			}
		}
		return 
	}

	// if a targetSet is passed, then assume it is reliable, otherwise create one
	// and initialize it with the current target contents.
	if  == nil {
		 = make(map[*html.Node]bool, len())
		for ,  := range  {
			[] = true
		}
	}
	for ,  := range  {
		if ![] {
			 = append(, )
			[] = true
		}
	}

	return 
}

// Loop through a selection, returning only those nodes that pass the predicate
// function.
func grep( *Selection,  func( int,  *Selection) bool) ( []*html.Node) {
	for ,  := range .Nodes {
		if (, newSingleSelection(, .document)) {
			 = append(, )
		}
	}
	return 
}

// Creates a new Selection object based on the specified nodes, and keeps the
// source Selection object on the stack (linked list).
func pushStack( *Selection,  []*html.Node) *Selection {
	 := &Selection{, .document, }
	return 
}